<?PHP
session_start();
require("../../conexao/ConectaMysql.class.php");
require("../../conexao/ControleAcesso.class.php");

ControleAcesso::verificaSessao();

$ACE_FUNid 	= $_SESSION['FUNid'];
$ACE_SETid 	= $_SESSION['SETid'];
$ACE_PRIVid = $_SESSION['PRIVid'];

$conexao = new ConectaMysql();

if (isset($_POST['txtbuscar'])) {
	$txtbuscar = $_POST['txtbuscar'];

}else if (isset($_GET['txtbuscar'])) {
	$txtbuscar = $_GET['txtbuscar'];	
}else{
	$txtbuscar = "";	
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Helpdesk</title>
<link href="../../estrutura/css/datagrid/light/estilo.css" rel="stylesheet" type="text/css" media="screen" />
<link href="../../estrutura/css/estruturaTotal.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div class="headerModulo"><img src="../../estrutura/imagens/ico_confirmacao.png" alt="" /> &nbsp;Registros em Aberto</div>
<div class="filtros" style="display: block;">
 <form id="pesquisar" method="post" action="formulario_abertos.php" name="pesquisar">
      <fieldset>
	     <span class="linha">
           <label class="preField" for="tipoServico">Localizar por:</label>
           <input id="txtbuscar" name="txtbuscar" type="text" value="<?php echo $txtbuscar; ?>"  size="40" class="caixa_texto">
           <input name="buscar" type="submit" id="buscar" value="Buscar" class="caixa_texto">
	     </span>
      </fieldset>
 </form>
</div>
<div class="datagrid_conteudo" id="datagrid_conteudo">
  <form id="datagrid_form_menus" name="datagrid_form_menus" action="formulario.php" onsubmit="return false">
    <table class="datagrid">
          
		<?php 
	 
		if ($txtbuscar != '') {
 			$txtbuscar = addslashes($txtbuscar);
			$sql_WHERE_txtbuscar = "((tb_helpdesk.HELPtitulo like '%$txtbuscar%') OR
			 						 (tb_helpdesk.HELPtitulo like '%$txtbuscar%') OR
									 (tb_categoria.CATdescricao like '%$txtbuscar%') OR									 
									 (tb_setor.SETsigla like '%$txtbuscar%') OR
									 (tb_funcionario.FUNnome like '%$txtbuscar%') OR
									 (tb_funcionario.FUNsobrenome like '%$txtbuscar%')) AND ";
		} else {
			
			$sql_WHERE_txtbuscar = "";
		}
			
		$queryHelpdesk = "SELECT		
							tb_helpdesk.HELPid, 
							tb_helpdesk.HELPtitulo, 
							tb_categoria.CATdescricao,
							tb_funcionario.FUNid,
							tb_funcionario.FUNnome,
							tb_funcionario.FUNsobrenome,
							tb_setor.SETid,
							tb_setor.SETsigla,
							tb_gestao.GESTdatainicial 
							
						  FROM  tb_helpdesk
							
							INNER JOIN tb_categoria ON
							tb_helpdesk.HELP_CATid = tb_categoria.CATid
							
							INNER JOIN tb_gestao ON
							tb_helpdesk.HELPid = tb_gestao.GEST_HELPid
							
							INNER JOIN tb_situacao ON
							tb_gestao.GEST_SITid = tb_situacao.SITid
							
							INNER JOIN tb_funcionario ON
							tb_helpdesk.HELP_FUNid = tb_funcionario.FUNid
							
							INNER JOIN tb_acesso ON
							tb_acesso.ACE_FUNid = tb_funcionario.FUNid
							
							INNER JOIN tb_privilegio ON
							tb_privilegio.PRIVid = tb_acesso.ACE_PRIVid
							
							INNER JOIN tb_setor ON
							tb_funcionario.FUN_SETid = tb_setor.SETid 
							
						  WHERE ($sql_WHERE_txtbuscar 
								   
							(tb_situacao.SITid = '1') and ((tb_setor.SETid = '$ACE_SETid') or ($ACE_PRIVid = '1') or (tb_helpdesk.HELP_DEST_SETid = '$ACE_SETid')))
							
						  ORDER BY tb_gestao.GESTdatainicial DESC";
								 
			 
		$quantreg = $conexao->rowCount($queryHelpdesk);	
		?>

        <tr>
        	<td colspan="7" class="alinCentro"><?php include("../../estrutura/paginacao.php"); ?></td>
        </tr>
        
        <tr class="header">
            <th width="16%" class=" alinCentro " style="">Funcionário</th>
            <th width="12%" class=" alinCentro " style="">Setor Remente</th>
          	<th width="16%" class=" alinCentro " style="">Titúlo </th>
            <th width="12%" class=" alinCentro " style="">Categoria</th>
            <th width="19%" class=" alinCentro " style="">Setor Responsável</th>
            <th width="16%" class=" alinCentro " style="">Data Cadastro</th>
            <th width="9%" class=" alinCentro " style="">Op&ccedil;&otilde;es</th>
        </tr>

		<?php
	
		$result_helpdesk = $conexao->executaQuery("$queryHelpdesk LIMIT $inicial, $numreg");
		  
		foreach($result_helpdesk as $row_helpdesk){ 
		
			$HELPid       		= $row_helpdesk["HELPid"];
			$HELPtitulo			= $row_helpdesk["HELPtitulo"];
			$CATdescricao		= $row_helpdesk["CATdescricao"];
			$FUNid				= $row_helpdesk["FUNid"];
			$FUNnome			= $row_helpdesk["FUNnome"];
			$FUNsobrenome		= $row_helpdesk["FUNsobrenome"];
			$SETid				= $row_helpdesk["SETid"];
			$SETsigla			= $row_helpdesk["SETsigla"];
			$GESTdatainicial	= $row_helpdesk["GESTdatainicial"];

			
			$setor_destinatario = "SELECT * FROM tb_setor 
								  INNER JOIN tb_helpdesk ON 
								  tb_helpdesk.HELP_DEST_SETid = tb_setor.SETid 
								  WHERE tb_helpdesk.HELPid = '$HELPid'";
								  
			$result_setor_dest = $conexao->executaQuery($setor_destinatario);
			
			foreach($result_setor_dest as $row_setor_dest){
				$SETid_dest = $row_setor_dest['SETid'];
				$SETsigla_dest = $row_setor_dest['SETsigla'];
				
			}
		?>	
        <tr id="datagrid_menus_linha_0" class="">
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$FUNnome $FUNsobrenome"; ?></span></td>          
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$SETsigla"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$HELPtitulo"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$CATdescricao"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$SETsigla_dest"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$GESTdatainicial"; ?></span></td>        
            <td class=" alinCentro ">
                <a title="Visualizar" class="botaoDatagrid visualizar" href="visualizar_helpdesk.php?HELPid=<?php echo $HELPid;?>"></a>&nbsp; 
                <?php if((($ACE_PRIVid <> 3) and ($ACE_SETid == $SETid)) || ($ACE_FUNid == $FUNid)){ ?>
                    <a title="Alterar" class="botaoDatagrid alterar" href="alterar_helpdesk.php?HELPid=<?php echo $HELPid;?>"></a>&nbsp; 
                <?php } ?>                
                <?php if($ACE_SETid == $SETid_dest){?>
                    <a title="Responder" class="botaoDatagrid cadastrar" href="../resposta/inserir_resposta.php?HELPid=<?php echo "$HELPid";?>"></a> 
                <?php }?>                
            </td>
        </tr>
		<?php } ?>  
    </table>     
  </form>
 </div> 
</body>
</html>
